package edu.msg.hrappy.backend.model;



import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;

@MappedSuperclass
public class AbstractModel {
    
    private static final int PRIME = 31;
    private String             uuid;
    
    @Override
    public int hashCode () {
        int result = 1;
        result = PRIME * result + ((this.getUuid () == null) ? 0 : this.getUuid ().hashCode ());
        return result;
    }
    
    @Override
    public boolean equals (final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass () != obj.getClass ()) {
            return false;
        }
        AbstractModel other = (AbstractModel) obj;
        if (this.getUuid () == null) {
            if (other.getUuid () != null) {
                return false;
            }
        } else if (!this.getUuid ().equals (other.getUuid ())) {
            return false;
        }
        return true;
    }
    
    
    @PrePersist
    private void ensureUuid(){
    	 if (uuid == null) {
             uuid = UUID.randomUUID ().toString();
         }
    	
    }
    
    
    
    @Column(name="uuid")
    public String getUuid () {
    	ensureUuid();
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    

}

